﻿using CPPEI.Coolzon.Event.NCL;
using CPPEI.Coolzon.Event.UI;
using CPPEI.Coolzon.Metadata;
using CPPEI.Coolzon.UI;
using CPPEI.Coolzon.UI.Data;
using CPPEI.Coolzon.UI.Event;
using CPPEI.Coolzon.UI.Views.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Theme.Fashion.MainViewControl
{
    /// <summary>
    /// OperationNavigation.xaml 的交互逻辑
    /// </summary>
    public partial class OperationNavigation : UserControl
    {
        private string _sceneName;
        private int _selectionState;
        private int _lectureState;
        private int _interactionState;
        private int _functionState;
        private bool? _isInteractionButton;

        public bool IsShowMagnifier = false;
        public bool IsShowLaserPen = false;

        public OperationNavigation()
        {
            InitializeComponent();
        }
        //激光笔
        private void LaserPenImg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!IsShowLaserPen)
            {
                laserPenImg.Style = (Style)dockPanel.Resources["laserPenSelect"];
                UIEventBus.Publish(new UIShowLaserPointEvent(true));
                IsShowLaserPen = true;
            }
            else
            {
                UIEventBus.Publish(new UIShowLaserPointEvent(false));
            }

            if (IsShowMagnifier)
            {
                UIEventBus.Publish(new UIShowMagnifierEvent(false));
            }
        }
        //上一场景
        private void preImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            preImage.Style = (Style)dockPanel.Resources["preSelect"];
            if (IsShowMagnifier)
            {
                UIEventBus.Publish(new UIShowMagnifierEvent(false));
            }
            if (IsShowLaserPen)
            {
                UIEventBus.Publish(new UIShowLaserPointEvent(false));
            }

            if (UI.UIContext.Current.OpenedSceneList != null && UI.UIContext.Current.OpenedSceneList.Count >= 2)
            {
                //先取出当前场景，再取出上一场景
                UI.UIContext.Current.OpenedSceneList.Pop();
                string preSceneName = UI.UIContext.Current.OpenedSceneList.Pop();
                Scene preScene = SolutionUtil.GetScene(UIContext.Current.Solution, preSceneName);
                NCLEventBus.Publish(new NCLOpenSceneEvent(SwitchMode.Replace, "", preScene.Name, null));
            }
            preImage.Style = (Style)dockPanel.Resources["pre"];
        }
        //打开放大镜
        private void magnifierImg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!IsShowMagnifier)
            {
                magnifierImg.Style = (Style)dockPanel.Resources["magnifierSelect"];
                UIEventBus.Publish(new UIShowMagnifierEvent(true));
                IsShowMagnifier = true;
            }
            else
            {
                UIEventBus.Publish(new UIShowMagnifierEvent(false));
            }
            if (IsShowLaserPen)
            {
                UIEventBus.Publish(new UIShowLaserPointEvent(false));
            }
        }
        //重置场景
        private void refreshImg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            refreshImg.Style = (Style)dockPanel.Resources["refreshSelect"];
            if (!string.IsNullOrEmpty(_sceneName))
            {
                Common.EventBus.Instance.Publish(new BeginResetSceneEvent(_sceneName));
            }
            if (IsShowMagnifier)
            {
                UIEventBus.Publish(new UIShowMagnifierEvent(false));
            }
            if (IsShowLaserPen)
            {
                UIEventBus.Publish(new UIShowLaserPointEvent(false));
            }
            refreshImg.Style = (Style)dockPanel.Resources["refresh"];
        }
        private void SetNavButBgImage(Grid gd)
        {
            foreach (UIElement element in dockPanel.Children)
            {
                if (element is Grid)
                {
                    Grid resSp = element as Grid;
                    if (resSp.Tag == null || resSp == spInteraction) continue;

                    if (resSp.Name.Equals(gd.Name))
                    {
                        resSp.Style = (Style)dockPanel.Resources["gdSelectFunc"];
                    }
                    else
                    {
                        resSp.Style = (Style)dockPanel.Resources["gdFunc"];
                    }
                }
            }
        }
        private void RestoreNav()
        {
            foreach (UIElement element in dockPanel.Children)
            {
                if (element is Grid)
                {
                    Grid resgd = element as Grid;
                    resgd.Style = (Style)dockPanel.Resources["gdFunc"];
                }
            }

            _selectionState = 0;
            _lectureState = 0;
            //_interactionState = 0;
            _functionState = 0;
            _isInteractionButton = null;
        }
        private void spScene_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavLeftButtonDown("Scene selection", ref _selectionState, spScene);
            _lectureState = 0;
            _functionState = 0;
        }

        private void spText_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavLeftButtonDown("Lecture notes", ref _lectureState, spText);
            _selectionState = 0;
            _functionState = 0;
        }

        /// <summary>
        /// 显示按钮交互模式
        /// </summary>
        private void ShowButtonInteractionMode()
        {
            if (_interactionState == 0)
            {
                return;
            }
            spInteraction_MouseLeftButtonDown(null, null);
        }

        private void spInteraction_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //NavLeftButtonDown("Free interaction", ref _interactionState, spInteraction);
            //_interactionState=0 按钮模式,_interactionState=1自由模式
            if (!_isInteractionButton.HasValue || _isInteractionButton.Value)
            {
                if (_interactionState == 0)
                {
                    Common.EventBus.Instance.Publish(new SetShowModelEvent { IsFreeModel = true });
                    Common.EventBus.Instance.Publish(new CloseNavEvent());
                    Common.EventBus.Instance.Publish(new ShowRecordEvent());
                    _interactionState = 1;
                }
                else if (_interactionState == 1)
                {
                    Common.EventBus.Instance.Publish(new SetShowModelEvent { IsFreeModel = false });
                    Common.EventBus.Instance.Publish(new ShowRecordEvent());
                    _interactionState = 0;
                }
                SetInterationMode();
            }
            else
            {
                Common.EventBus.Instance.Publish(new CloseNavEvent());
                _isInteractionButton = true;
            }
            SetNavButBgImage(spInteraction);
            _selectionState = 0;
            _lectureState = 0;
            _functionState = 0;
        }
        public void SetInterationMode()
        {
            CPPEI.Coolzon.UI.Views.RenderView renderView = (CPPEI.Coolzon.UI.Views.RenderView)UI.UIContext.Current.RenderView;
            if (renderView.Visibility == Visibility.Hidden)
            {
                interactionImg.Source = new BitmapImage(
                        new Uri("/CPPEI.Coolzon.Theme.Fashion;component/Images/3.png", UriKind.Relative));
                interactionText.Text = "自由交互模式";
            }
            else
            {
                interactionImg.Source = new BitmapImage(
                    new Uri("/CPPEI.Coolzon.Theme.Fashion;component/Images/55.png", UriKind.Relative));
                interactionText.Text = "按钮交互模式";
            }
        }
        private void spFunction_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavLeftButtonDown("System function", ref _functionState, spFunction);
            _selectionState = 0;
            _lectureState = 0;
        }
        private void NavLeftButtonDown(string navName, ref int navState, UIElement navElement)
        {
            if (navState == 0)
            {
                Coolzon.Common.EventBus.Instance.Publish(new FastionNavChangeEvent { NavName = navName });
                navState = 1;
                SetNavButBgImage((Grid)navElement);
                _isInteractionButton = false;
            }
            else if (navState == 1)
            {
                Common.EventBus.Instance.Publish(new CloseNavEvent());
                navState = 0;
                _isInteractionButton = true;
                //SetNavButBgImage(spInteraction);
            }
        }

        private void magnifierImg_TouchUp(object sender, TouchEventArgs e)
        {
            e.Handled = true;
        }

        private void magnifierImg_MouseEnter(object sender, MouseEventArgs e)
        {
            magnifierImg.Style = (Style)dockPanel.Resources["magnifierSelect"];
        }

        private void magnifierImg_MouseLeave(object sender, MouseEventArgs e)
        {
            if (!IsShowMagnifier)
            {
                magnifierImg.Style = (Style)dockPanel.Resources["magnifier"];
            }

        }

        private void laserPenImg_MouseLeave(object sender, MouseEventArgs e)
        {
            if (!IsShowLaserPen)
            {
                laserPenImg.Style = (Style)dockPanel.Resources["laserPen"];
            }
        }

        private void laserPenImg_MouseEnter(object sender, MouseEventArgs e)
        {
            laserPenImg.Style = (Style)dockPanel.Resources["laserPenSelect"];
            e.Handled = true;
        }

        private void preImage_TouchUp(object sender, TouchEventArgs e)
        {
            preImage.Style = (Style)dockPanel.Resources["pre"];
        }

        private void preImage_TouchEnter(object sender, TouchEventArgs e)
        {
            preImage.Style = (Style)dockPanel.Resources["preSelect"];
        }

        private void refreshImg_TouchUp(object sender, TouchEventArgs e)
        {
            refreshImg.Style = (Style)dockPanel.Resources["refresh"];
        }

        private void refreshImg_TouchEnter(object sender, TouchEventArgs e)
        {
            refreshImg.Style = (Style)dockPanel.Resources["refreshSelect"];
        }

        private void preImage_MouseEnter(object sender, MouseEventArgs e)
        {
            preImage.Style = (Style)dockPanel.Resources["preSelect"];
        }

        private void preImage_MouseLeave(object sender, MouseEventArgs e)
        {
            preImage.Style = (Style)dockPanel.Resources["pre"];
        }

        private void refreshImg_MouseEnter(object sender, MouseEventArgs e)
        {
            refreshImg.Style = (Style)dockPanel.Resources["refreshSelect"];
        }

        private void refreshImg_MouseLeave(object sender, MouseEventArgs e)
        {
            refreshImg.Style = (Style)dockPanel.Resources["refresh"];
        }
    }
}
